package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.dao.AddressDao;
import com.bed.lovewithpatientbed.dao.HospitalDao;
import com.bed.lovewithpatientbed.pojo.Address;
import com.bed.lovewithpatientbed.pojo.AddressExample;
import com.bed.lovewithpatientbed.pojo.Hospital;
import com.bed.lovewithpatientbed.pojo.HospitalExample;
import com.bed.lovewithpatientbed.service.AddressService;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yg
 * @version 1.0.0
 * @ClassName HospitalService.java
 * @Description TODO
 */
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressDao addressDao;
    @Resource
    private HospitalDao hospitalDao;

    /**
         * @title 条件查询地址id
         * @description 根据提供的省、市和名字查询具体的AddressId字段信息
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public List<Address> getAddressBySelect(Address address) {
        AddressExample addressExample=new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();

        //根据省分名称查询
        if(address.getProvince()!=null&&!"".equals(address.getProvince())){
            criteria.andProvinceEqualTo(address.getProvince());
        }

        //根据市名称查询
        if(address.getCity()!=null&&!"".equals(address.getCity())){
            criteria.andCityEqualTo(address.getCity());
        }

        //根据市区名称查询
        if(address.getDistrict()!=null&&!"".equals(address.getDistrict())){
            criteria.andDistrictEqualTo(address.getDistrict());
        }

        List<Address> addressList = addressDao.selectByExample(addressExample);
        return addressList;
    }

    /**
         * @title 新增地址信息
         * @description 新增医院的时候会在其他地区新增医院，需要用到
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public int addAddress(Address address) {
        int i = addressDao.insert(address);
        return i;
    }

    /**
         * @title 修改地址信息
         * @description 对医院地址进行修改的时候
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public int updatAddress(Address address) {
        int i = addressDao.updateByPrimaryKey(address);
        return i;
    }



    /**
     * @description 查出所有地址信息
     * @author tyl
     * @updateTime 2020/10/14 16:04
     */
    @Override
    public List<Address> selectAll() {
        List<Address> addressList = addressDao.selectByExample(null);
        return addressList;
    }
    /**
     * @description 按省份查询地址
     * @author tyl
     * @updateTime 2020/10/14 19:12
     */
    @Override
    public List<Address> selectByProvince(String province) {
        /*添加条件*/
        AddressExample addressExample=new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();

        if(province!=null&&!"".equals(province)){
            criteria.andProvinceEqualTo(province);
        }
        List<Address> addressList = addressDao.selectByExample(addressExample);
        return addressList;
    }
    /**
     * @description 查出所有地址信息（去重）
     * @author tyl
     * @updateTime 2020/10/14 16:04
     */
    @Override
    public List<Address> selectByDistinct() {
        List<Address> addressList = addressDao.selectByDistinct();
        return addressList;
    }
    /**
         * @title 根据地址id查询地址信息
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public List<Address> getAddressByID(List<Integer> adids) {
        List<Address> addressList=new ArrayList<>();
        if (adids.size()!=0){
            for (Integer adid:adids
                 ) {
                Address address = addressDao.selectByPrimaryKey(adid);
                addressList.add(address);
            }
        }
        return addressList;
    }

    @Override
    public Address getById(Integer adid) {
        return addressDao.selectByPrimaryKey(adid);
    }
    /**
     * @description 根据省份查地址的集合
     * @author tyl
     * @updateTime 2020/10/16 11:52 
     * @param province:省份的集合
     */
    @Override
    public List<Address> selectByProvinceList(List<String> province){
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceIn(province);
        List<Address> addressList = addressDao.selectByExample(example);
        return addressList;
    }
    /**
     * @title
     * @description 根据省份查询城市
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Address> selectCity(String province) {
        List<Address> addressList = addressDao.selectCity(province);
        return addressList;
    }

    /**
     * @title selectDistrict
     * @description 根据城市查询区
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Address> selectDistrict(String city) {
        List<Address> addressList = addressDao.selectDistrict(city);
        return addressList;
    }

    /**
     * @title selectHospital
     * @description 根据区查询医院
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Hospital> selectHospital(String district) {
        List<Hospital> hospitalList = addressDao.selectHospital(district);
        System.out.println(hospitalList);
        return hospitalList;
    }

    /**
     * @title selectByCity
     * @description 根据城市查地址
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Address> selectByCity(String city) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andCityEqualTo(city);
        List<Address> addressList = addressDao.selectByExample(addressExample);
        return addressList;
    }

    /**
     * @title selectByDistrict
     * @description 根据区查地址
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Address> selectByDistrict(String district) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andDistrictEqualTo(district);
        List<Address> addressList = addressDao.selectByExample(addressExample);
        return addressList;
    }

    /**
         * @title 添加地址信息
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public int insertAddress(Address address) {
        AddressExample addressExample=new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (address.getProvince()!=null&&!"".equals(address.getProvince())){
            if (address.getCity()!=null&&!"".equals(address.getCity())){
                if (address.getDistrict()!=null&&!"".equals(address.getDistrict())){
                    criteria.andProvinceEqualTo(address.getProvince());
                    criteria.andCityEqualTo(address.getCity());
                    criteria.andDistrictEqualTo(address.getDistrict());
                }
            }
        }
        if ((addressDao.selectByExample(addressExample)).size()==0){
            addressDao.insertSelective(address);
            return (addressDao.selectByExample(addressExample)).get(0).getAddressId();

        }else{
            return (addressDao.selectByExample(addressExample)).get(0).getAddressId();
        }

    }

    @Override
    public List<Address> selectByAddCon(Address address) {
        AddressExample addressExample=new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (address.getProvince()!=null&&!"".equals(address.getProvince())){
            if (address.getCity()!=null&&!"".equals(address.getCity())){
                if (address.getDistrict()!=null&&!"".equals(address.getDistrict())){
                    criteria.andProvinceEqualTo(address.getProvince());
                    criteria.andCityEqualTo(address.getCity());
                    criteria.andDistrictEqualTo(address.getDistrict());
                }
            }
        }
        return addressDao.selectByExample(addressExample);
    }

    @Override
    public List<Address> getProvince() {

        List<Address> provincelist = addressDao.selectByDistinct();
        return provincelist;
    }

    @Override
    public List<Address> getCity(String province) {
        List<Address> citylist = addressDao.selectByCity1(province);
        return citylist;
    }

    @Override
    public List<Address> getDistinct(String province,String city) {


        List<Address> distinctlist = addressDao.selectByCity2(province,city);
        return distinctlist;
    }



}
